//
//  ShareItemView.m
//  CaiCaiYunNotes
//
//  Created by user on 13-11-15.
//  Copyright (c) 2013年 leo. All rights reserved.
//

#import "ShareItemView.h"

@interface ShareItemView()

@property (nonatomic, assign) int y;
@property (nonatomic, copy) void (^Click)(NSInteger);

@end

@implementation ShareItemView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code

    }
    return self;
}

-(id)initWithContents:(NSArray *)contents Click:(void (^)(NSInteger))click cancel:(void (^)())cancel{
    if (self = [super init]) {
        
        self.Click = click;
        NSMutableArray *arrType = [NSMutableArray arrayWithCapacity:10];
        [arrType addObjectsFromArray:contents];
        
        self.backgroundColor = [UIColor whiteColor];
        
        NSInteger num = [arrType count];
        
        UIButton *but[num];
        UILabel *lab[num];
        
        self.y = 20;
        
        CGFloat size = 45;
        CGFloat allIconWidth = 250 * __MAIN_RATIO_320;
        CGFloat iconW = allIconWidth / 3;
        CGFloat left = (__MAIN_WIDTH - allIconWidth) / 2 + (iconW - size) / 2;
        CGFloat x = left;
        
//        NSInteger count = 3;
        
        [[[UIView viewWithFrame:CGRectMake(0, 0, __MAIN_WIDTH, 100 + self.y)] resetBackgroundColor:COLOR_HEXSTRING(@"#eeeeee")] setupOnView:self];
        
        
        for (int i = 0; i < num; i++) {
            but[i] = [UIButton buttonWithType:UIButtonTypeCustom];
            [self addSubview:but[i]];
            NSInteger type = [[arrType objectAtIndex:0] integerValue];
            [arrType removeObjectAtIndex:0];
            but[i].tag = type;
            [but[i] addTarget:self action:@selector(shareClick:) forControlEvents:UIControlEventTouchUpInside];
            NSString *strText = [NSString stringWithFormat:@"share_%zd",type];
            
            but[i].backgroundColor = [UIColor colorWithPatternImage:ImageNamed(strText)];
            but[i].frame = CGRectMake(x, _y, size, size);
            
            lab[i] = [[UILabel alloc] initWithFrame:CGRectMake(x - 11, _y + 60, 80, 20)];
            lab[i].centerX = but[i].centerX;
            [self addSubview:lab[i]];
            
            lab[i].textColor = [UIColor darkGrayColor];
            //            lab[i].contentMode = UIViewContentModeCenter;
            lab[i].textAlignment = NSTextAlignmentCenter;
            lab[i].font = [UIFont systemFontOfSize:12.];
            lab[i].backgroundColor = [UIColor clearColor];
            strText = [NSString stringWithFormat:@"share_%zd",type];
            lab[i].text = NSLocalizedStringFromTable(strText, @"Loc_Share", nil);

//            if (x + 80 > 225) {
//                x = left;
//                _y += 100;
//            }else{
                x += iconW;
//            }
        }
        
        _y += 100;
        
        CGFloat width = __MAIN_WIDTH;
        
        UIButton *cancelB = [ViewFactory buttonWithFrame:CGRectMake(0, _y, width - 2, 47) title:@"取消" normalColor:[UIColor whiteColor] highlightColor:[UIColor whiteColor] disabledColor:nil inView:self action:cancel];
        [cancelB setTitleColor:[Theme wordGrayColor] forState:UIControlStateNormal];
        _y += cancelB.height;
        self.frame = CGRectMake(0, 0, width, _y);
    }
    return self;
}

-(void)shareClick:(id)sender{
    NSInteger type = ((UIView *)sender).tag;
    if (self.Click) {
        self.Click(type);
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
